package com.jr.controller;

import com.jr.pojo.PetOwner;
import com.jr.service.PetOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/pet-owner")
@CrossOrigin(origins = "*")
public class PetOwnerController {

    @Autowired
    private PetOwnerService petOwnerService;

    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> addPetOwner(@RequestBody PetOwner petOwner) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = petOwnerService.addPetOwner(petOwner);
            if (success) {
                result.put("success", true);
                result.put("message", "添加成功");
                result.put("data", petOwner);
            } else {
                result.put("success", false);
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/{ownerId}")
    @ResponseBody
    public Map<String, Object> getPetOwnerById(@PathVariable Integer ownerId) {
        Map<String, Object> result = new HashMap<>();
        try {
            PetOwner petOwner = petOwnerService.getPetOwnerById(ownerId);
            if (petOwner != null) {
                result.put("success", true);
                result.put("data", petOwner);
            } else {
                result.put("success", false);
                result.put("message", "宠物主人信息不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/user/{userId}")
    @ResponseBody
    public Map<String, Object> getPetOwnerByUserId(@PathVariable Integer userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            PetOwner petOwner = petOwnerService.getPetOwnerByUserId(userId);
            if (petOwner != null) {
                result.put("success", true);
                result.put("data", petOwner);
            } else {
                result.put("success", false);
                result.put("message", "宠物主人信息不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @PutMapping("/update")
    @ResponseBody
    public Map<String, Object> updatePetOwner(@RequestBody PetOwner petOwner) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = petOwnerService.updatePetOwner(petOwner);
            if (success) {
                result.put("success", true);
                result.put("message", "更新成功");
            } else {
                result.put("success", false);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/all")
    @ResponseBody
    public Map<String, Object> getAllPetOwners() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<PetOwner> petOwners = petOwnerService.getAllPetOwners();
            result.put("success", true);
            result.put("data", petOwners);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @DeleteMapping("/{ownerId}")
    @ResponseBody
    public Map<String, Object> deletePetOwner(@PathVariable Integer ownerId) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = petOwnerService.dropPetOwner(ownerId);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }
}
